<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /**
         * 管理事件的函数
         * 事件只有在触发的时候才会执行，在没被触发之前函数被放在事件管理模块中
         * 用一个对象模拟
         * eventObj = {
         *      'click' :[fn1,fn2,....]
         * }
         * 
         * 
          */
         class Myevent {
            constructor(eventObj) {
                this.eventObj = {}; 
            }
            addEvent(eventName, fn) {
                
                if(typeof this.eventObj[eventName] === 'undefined') {
                    this.eventObj[eventName] = [];
                }
                this.eventObj[eventName].push(fn);
            }
            trigger(eventName){
                if(typeof this.eventObj[eventName] !== 'undefined') {
                    this.eventObj[eventName].forEach(fn => {
                        fn();
                    })
                }else {
                    throw Error('传参错误')
                }
            }
         }
        let myevent = new Myevent();
        myevent.addEvent('myevent', function () {
            console.log(111);
        });
        myevent.addEvent('myevent', function () {
            console.log(222);
        });
        myevent.addEvent('myevent1', function () {
            console.log(333);
        });
        myevent.trigger('myevent');
        myevent.trigger('myevent1');
    </script>
</body>
</html>